#coding: utf-8
from django.db import models
from django.contrib.auth.models import User
from datetime import datetime
from django.core.urlresolvers import reverse
from django.utils.html import urlize
# import cgi
# from loginza_auth import update_loginza_userinfo
class Topic(models.Model):
    user = models.ForeignKey(User)
    title = models.CharField(u'Название', max_length = 256)
    public = models.BooleanField(u'Видна всем', default = True)
    guest_ok = models.BooleanField(u'Пишут все', default = True)
    created = models.DateTimeField(u'Дата создания', default = datetime.now())
    edited = models.DateTimeField(u'Последние изменения', default = datetime.now())
    published = models.BooleanField(u'Опубликовано', default = True)
    def __unicode__(self):
        return self.title
    def access_comment(self):
        if self.public and self.guest_ok: return u'В эту тему могут писать все.'
        if self.public and not self.guest_ok: return u'В эту тему может писать только ее хозяин - {0}.'.format(self.user.userprofile)
        if not self.public: return u'Эта тема видна только владельцу - {0}}'.format(self.user.userprofile)

    class Meta:
        ordering = ['-edited']
        db_table = 'main_topic'
    
class UnBlog(models.Model):
    user = models.ForeignKey(User)
    topic = models.ForeignKey(Topic)
    text = models.TextField(u'Текст', max_length = 50000)
    datetime = models.DateTimeField(u'Дата', default = datetime.now())
    published = models.BooleanField(u'Опубликовано', default = True)
    corrected = models.DateTimeField(u'Дата', blank = True, null = True)
    class Meta:
        ordering = ['-datetime']
        db_table = 'main_unblog'
    def text_safe(self):
        return escape_safe(self.text)

    def save(self):
        self.topic.edited = datetime.now()
        self.text = escape_html_to_db(self.text)
        self.topic.save()
        super(UnBlog, self).save()
    def get_permalink(self):
        return reverse('unblog_post', args = [self.topic.id, self.id])
    def __unicode__(self):
        return escape_safe(u"{0} {1}: {2}".format(self.user.get_profile(), self.datetime, self.text_safe()[:30]))
    def show(self):
        return urlize(self.text)

def escape_html_to_db(s):
    s = s.replace("'", "&quot;").replace('"', "&quot;")
    s = s.replace("<", "&lt;").replace(">", "&gt;")
    s = s.replace("&lt;b&gt;", "<b>").replace(r"&lt;/b&gt;", "</b>")
    s = s.replace("&lt;pre&gt;", "<pre>").replace(r"&lt;/pre&gt;", "</pre>")
    s = s.replace("&lt;code&gt;", "<code>").replace(r"&lt;/code&gt;", "</code>")
    s = s.replace("&lt;i&gt;", "<i>").replace(r"&lt;/i&gt;", "</i>")
    s = s.replace("&lt;u&gt;", "<u>").replace(r"&lt;/u&gt;", "</u>")

    return s
    # s = cgi.escape(s, True)
    # s = s.replace("\n\r", "<br>")
    # s = s.replace("\r\n", "<br>")
    # s = s.replace("\n", "<br>")
    # s = s.replace("\r", "<br>")
    # return s

def escape_safe(s):
    # s = cgi.escape(s, True)
    s = s.replace("'", "&quot;").replace('"', "&quot;")
    s = s.replace("<", "&lt;").replace(">", "&gt;")
    s = s.replace("\n", "")
    s = s.replace("\r", "")
    return s
